package com.fone.player.online.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.entity.LabelInfo;
import com.fone.player.online.activity.ColumnActivity;
import com.fone.player.online.activity.CommonActivity;
import com.fone.player.online.activity.GridModeActivity;
import com.fone.player.online.activity.HotActivity;
import com.fone.player.online.activity.LeaderboardColumnActivity;
import com.fone.player.online.activity.LiveActivity;
import com.fone.player.online.activity.VipActivity;

/**
 * 更多页面中的适配器。
 */
public class ChannelAdapter extends ArrayAdapter<LabelInfo> {
	
	/** 日志标签 */
//	private static final String TAG = "ChannelAdapter";
	
	private int resource;  
	private Context context;
	
	/** 在线频道页模板类型 */
	private static final int CHANNEL_LIVE 				= 9;
	private static final int CHANNEL_VERTICAL 			= 10;
	private static final int CHANNEL_HORIZONTAL 		= 11;
	private static final int CHANNEL_COMMON 			= 12;
	private static final int CHANNEL_COLUMN 			= 13;
	private static final int CHANNEL_VIP 				= 14;
	private static final int CHANNEL_LEADERBOARD 		= 16;
	
	public ChannelAdapter(Context context, int resource, int textViewResourceId,
			List<LabelInfo> channelList) {
		super(context, resource, textViewResourceId, channelList);
		this.context = context;
		this.resource = resource;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view = null;
		if(convertView == null){
			view = LayoutInflater.from(getContext()).inflate(resource, parent, false);
		}else{
			view = convertView;
		}
		
		TextView textView = (TextView)view.findViewById(R.more.title);
		CheckBox checkBox = (CheckBox)view.findViewById(R.more.check);
		ImageView fixedImg = (ImageView)view.findViewById(R.more.fixed);
		RelativeLayout layout = (RelativeLayout)view.findViewById(R.more.layout);
//		ImageView columnImg = (ImageView)view.findViewById(R.more.column);
		LabelInfo info = getItem(position);
		
		if(info.custom.equals("0")){
			checkBox.setVisibility(View.GONE);
			fixedImg.setVisibility(View.VISIBLE);
		}else{
			checkBox.setVisibility(View.VISIBLE);
			fixedImg.setVisibility(View.GONE);
		}
		
		textView.setText(info.name);
		
		//跳转到相应的2级别栏目页
		layout.setOnClickListener(new ColumnListener(info));
		
		return view;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		 return true;
		
	}
	
	private class ColumnListener implements OnClickListener{
		
		LabelInfo info;
		public ColumnListener(LabelInfo info) {
			super();
			this.info = info;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int type = Integer.valueOf(info.type);
			Intent intent = null;
			if (info.name.equals("全网热点")) {
				intent = new Intent(context, HotActivity.class);
				intent.putExtra("url", info.url);
				intent.putExtra("name", info.name);
				intent.putExtra("id", info.id);
				context.startActivity(intent);
				
			}
			else if(CHANNEL_COMMON == type){
				intent = new Intent(context, CommonActivity.class);
				intent.putExtra("url", info.url);
				intent.putExtra("name", info.name);
				intent.putExtra("id", info.id);
				context.startActivity(intent);
				
			}else if(CHANNEL_HORIZONTAL == type){
				intent = new Intent(context, GridModeActivity.class);
				intent.putExtra("url", info.url);
				intent.putExtra("name", info.name);
				intent.putExtra("id", info.id);
				intent.putExtra("showtype", 11);
				context.startActivity(intent);
				
			}else if(CHANNEL_LIVE == type){
				intent = new Intent(context, LiveActivity.class);
				intent.putExtra("url", info.url);
				intent.putExtra("name", info.name);
				intent.putExtra("id", info.id);
				
				context.startActivity(intent);
				
			}else if(CHANNEL_VERTICAL == type){
				intent = new Intent(context, GridModeActivity.class);
				intent.putExtra("url", info.url);
				intent.putExtra("name", info.name);
				intent.putExtra("id", info.id);
				intent.putExtra("showtype", 10);
				context.startActivity(intent);
				
			}else if(CHANNEL_VIP == type){
				intent = new Intent(context, VipActivity.class);
				intent.putExtra("url", info.url);
				intent.putExtra("name", info.name);
				intent.putExtra("id", info.id);
				context.startActivity(intent);
				
			}else if(CHANNEL_COLUMN == type){
				intent = new Intent(context, ColumnActivity.class);
				intent.putExtra("url", info.url);
				intent.putExtra("name", info.name);
				intent.putExtra("id", info.id);
				context.startActivity(intent);
				
			}else if(CHANNEL_LEADERBOARD == type){
				intent = new Intent(context, LeaderboardColumnActivity.class);
				intent.putExtra("url", info.url);
				intent.putExtra("name", info.name);
				intent.putExtra("id", info.id);
				context.startActivity(intent);
				
			}else{
				intent = new Intent(context, CommonActivity.class);
				intent.putExtra("url", info.url);
				intent.putExtra("name", info.name);
				intent.putExtra("id", info.id);
				context.startActivity(intent);
			}
		}
	}
	
}
